# coding=utf-8
import matplotlib.pyplot as plt

# 字体设置
from matplotlib.font_manager import FontProperties
from numpy import *
import math

font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)


# 2pi为周期  傅里叶级数
def fourier2Pi():
    # 定义区间
    x = arange(-pi, pi, 0.01)
    # 计算次数
    times = 50
    # 时间间隔
    stepTime = 1 / times
    # 原函数
    y = exp(x)
    # 傅里叶级数 非求和部分
    a0 = (exp(pi) - exp(-pi)) / pi
    FourierY = a0 / 2
    # 连续绘图
    for n in range(1, times + 1, 1):
        # 此处写傅里叶级数的系数
        an = (exp(pi) - exp(-pi)) * (-1) ** n / ((1 + n ** 2) * pi)
        bn = -n * pi * an

        y0 = an * cos(n * x) + bn * sin(n * x)
        FourierY = FourierY + y0
        plt.plot(x, FourierY, label="傅里叶函数")
        plt.plot(x, y, label="原来函数")
        plt.gray()
        plt.legend(prop=font_set)  # 字体更改
        plt.title(n)
        if n == times:
            plt.show(block=True)
        else:
            plt.show(block=False)
            plt.pause(stepTime)
            plt.clf()


# 泰勒展开
def Tarler(times, Range=50, stepTime=0.1):
    x = arange(-Range, Range, 0.01)

    f0 = 1
    x0 = 0
    TarlerFunction = f0
    PrimitiveFunction = exp(x)
    for n in arange(1, times + 1):
        # f(x)在x0处的n次导数值
        fn = 1
        TarlerFunction = TarlerFunction + fn * (x - x0) ** n / math.factorial(n)
        plt.plot(x, TarlerFunction, label="泰勒展开（泰勒级数）")
        plt.plot(x, PrimitiveFunction, label="原来函数")
        plt.xlim(-Range, Range)
        plt.ylim(-Range / 10, Range)
        plt.gray()
        plt.legend(prop=font_set)  # 字体更改
        plt.title(n)
        if n == times:
            plt.show(block=True)
        else:
            plt.show(block=False)
            plt.pause(stepTime)
            plt.clf()


# 2l为周期  傅里叶级数
def fourier2l(times):
    l = 2
    # 定义区间
    x = arange(-l, l, 0.01)
    # 时间间隔
    stepTime = 2 / times
    # 原函数
    y = x ** 2
    # 傅里叶级数 非求和部分
    a0 = 0
    FourierY = a0 / 2
    # 连续绘图
    for n in range(1, times + 1, 1):
        # 此处写傅里叶级数的系数
        an = 0
        bn = 8 * ((-1) ** (n + 1)) / (n * pi) + 16 * ((-1) ** n - 1) / ((n * pi) ** 3)
        y0 = an * cos(n * pi * x / l) + bn * sin(n * pi * x / l)

        FourierY = FourierY + y0
        plt.plot(x, FourierY, label="傅里叶函数")
        plt.plot(x, y, label="原来函数")
        plt.gray()
        plt.legend(prop=font_set)  # 字体更改
        plt.title(n)
        if n == times:
            plt.show(block=True)
        else:
            plt.show(block=False)
            plt.pause(stepTime)
            plt.clf()


# 极坐标
def polarPlot():
    theta = arange(0, 2 * pi, 0.01)
    r = 3 * cos(theta)
    # 开启极坐标
    plt.subplot(polar=True)
    plt.plot(theta, r)

    # 半径的范围
    plt.ylim(0, 5)
    plt.show()


# 三维曲线 仔细看的画用solidworks
def line3d():
    plt.style.use('ggplot')
    fig = plt.figure()
    # 三维图形
    ax = fig.gca(projection='3d')

    t = arange(-10, 10, 0.01)
    x = t + 1
    y = t ** 2 - 1
    z = 2 * t

    ax.plot(x, y, z)
    ax.set_xlabel('x', color='r')
    ax.set_ylabel('y', color='r')
    ax.set_zlabel('z', color='r')

    ax.set_xlim(0, 20)
    ax.set_ylim(0, 20)
    ax.set_zlim(0, 20)
    plt.show()


# fourier2l(50)
# polarPlot()
# line3d()
Tarler(25, 5)
